package com.qingzhu.controller.userserver;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qingzhu.pojo.User;
import com.qingzhu.pojovo.ResultVo;
import com.qingzhu.pojovo.UserVo;
import com.qingzhu.service.UserService;
import com.qingzhu.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/user/select")
public class UserSelectController extends HttpServlet {
    UserService userService = new UserServiceImpl();
    ObjectMapper objectMapper = new ObjectMapper();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("用户查找服务..");

        String userId = req.getParameter("id");
        UserVo userVo = userService.findById(Integer.parseInt(userId));
        ResultVo resultVo = userVo.getUser() != null ? new ResultVo(200,userVo):
                new ResultVo(1000,"未查询到用户信息");
        resp.getWriter().write(objectMapper.writeValueAsString(resultVo));
    }
}
